package models

import (
	"github.com/astaxie/beego/orm"
	"object/go_cms/db"
	"strconv"
	"time"
)

type UploadFileModels struct{}

type UploadFile struct {
	Id          uint64 `orm:"auto;pk;"`
	Type_id     int    `orm:"default(1)"`
	Upload_name string `orm:"size(255)"`
	File_name   string `orm:"size(255)"`
	Type        int    `orm:"size(2);default(0)"`
	Ext         string `orm:"size(255)"`
	Url         string `orm:"size(255)"`
	Path        string `orm:"size(255)"`
	Add_time    int64
	Size        float64 `orm:"digits(13);decimals(2);default(0.00)"`
	Local_type  int     `orm:"default(1);size(2)"`
	Type_name   string
}

func init() {
	orm.RegisterModel(new(UploadFile))
}

func (u *UploadFileModels) Get(id uint64) (UploadFile, error) {
	upload := UploadFile{}
	upload.Id = id
	o := orm.NewOrm()
	err := o.Read(&upload)
	return upload, err
}

//获取列表，分页
func (u *UploadFileModels) QueryByPage(maps map[string]interface{}) ([]UploadFile, int64) {
	var list []UploadFile
	var count []orm.Params
	var param []interface{}
	sq := db.GetBuilderMysql()
	sq.Name("upload_file as uf")

	sq.Join("upload_type as ut", "ut.id=uf.type_id", "left")
	sq.Field("uf.*", "ut.title as type_name")

	if maps["type_id"] != nil {
		sq.Where("type_id = ?")
		param = append(param, maps["type_id"])
	}
	if maps["type"] != nil {
		sq.Where("type = ?")
		param = append(param, maps["type"])
	}
	if maps["ext"] != nil {
		sq.Where("ext = ?")
		param = append(param, maps["ext"])
	}
	if maps["local_type"] != nil {
		sq.Where("local_type = ?")
		param = append(param, maps["local_type"])
	}
	if maps["upload_name"] != nil {
		sq.Where("upload_name LIKE ?")
		param = append(param, "%"+maps["upload_name"].(string)+"%")
	}
	if maps["order"] != nil {
		sq.Order("size", maps["order"].(string))
	} else {
		sq.Order("id", "desc")
	}

	if maps["limit"] == nil {
		maps["limit"] = "10"
	}
	if maps["page"] == nil {
		maps["page"] = "1"
	}
	page, _ := strconv.ParseInt(maps["page"].(string), 10, 64)
	limit, _ := strconv.ParseInt(maps["limit"].(string), 10, 64)
	o := orm.NewOrm()
	o.Raw(sq.Page(page, limit), param).QueryRows(&list)
	o.Raw(sq.Count(), param).Values(&count)
	coun, _ := strconv.ParseInt(count[0]["count"].(string), 10, 64)
	return list, coun
}

//添加
func (u *UploadFileModels) Insert(maps map[string]interface{}) (int64, error) {
	upload := UploadFile{}

	if maps["type_id"] != nil {
		upload.Type_id, _ = strconv.Atoi(maps["type_id"].(string))
	} else {
		upload.Type_id = 1
	}
	if maps["upload_name"] != nil {
		upload.Upload_name = maps["upload_name"].(string)
	}
	if maps["file_name"] != nil {
		upload.File_name = maps["file_name"].(string)
	}
	if maps["type"] != nil {
		upload.Type, _ = strconv.Atoi(maps["type"].(string))
	}
	if maps["ext"] != nil {
		upload.Ext = maps["ext"].(string)
	}
	if maps["url"] != nil {
		upload.Url = maps["url"].(string)
	}
	if maps["path"] != nil {
		upload.Path = maps["path"].(string)
	}
	if maps["size"] != nil {
		upload.Size, _ = strconv.ParseFloat(maps["size"].(string), 64)
	}
	if maps["local_type"] != nil {
		upload.Local_type, _ = strconv.Atoi(maps["local_type"].(string))
	}
	upload.Add_time = time.Now().Unix()
	o := orm.NewOrm()
	return o.Insert(&upload)
}

//删除
func (u *UploadFileModels) Delete(id uint64) (int64, error) {
	upload := UploadFile{}
	upload.Id = id
	o := orm.NewOrm()
	return o.Delete(&upload)
}
